 package handling.world;

 import handling.world.guild.MapleGuild;
 import handling.world.guild.MapleGuildAlliance;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import tools.MaplePacketCreator;
 import tools.packet.GuildPacket;





 public class WorldAllianceService
 {
/*  22 */   private static final Logger log = LogManager.getLogger(WorldAllianceService.class.getName());
   private final Map<Integer, MapleGuildAlliance> allianceList;
   private final ReentrantReadWriteLock lock;

   private WorldAllianceService() {
/*  27 */     log.info("正在启动[WorldAllianceService]");
/*  28 */     this.lock = new ReentrantReadWriteLock();
/*  29 */     this.allianceList = new LinkedHashMap<>();
/*  30 */     Collection<MapleGuildAlliance> allGuilds = MapleGuildAlliance.loadAll();
/*  31 */     for (MapleGuildAlliance alliance : allGuilds) {
/*  32 */       this.allianceList.put(Integer.valueOf(alliance.getId()), alliance);
     }
   }

   public static WorldAllianceService getInstance() {
/*  37 */     return SingletonHolder.instance;
   }

   public MapleGuildAlliance getAlliance(int allianceId) {
/*  41 */     MapleGuildAlliance ret = null;
/*  42 */     this.lock.readLock().lock();
     try {
/*  44 */       ret = this.allianceList.get(Integer.valueOf(allianceId));
     } finally {
/*  46 */       this.lock.readLock().unlock();
     }
/*  48 */     if (ret == null) {
/*  49 */       this.lock.writeLock().lock();
       try {
/*  51 */         ret = new MapleGuildAlliance(allianceId);
/*  52 */         if (ret == null || ret.getId() <= 0) {
/*  53 */           return null;
         }
/*  55 */         this.allianceList.put(Integer.valueOf(allianceId), ret);
       } finally {
/*  57 */         this.lock.writeLock().unlock();
       }
     }
/*  60 */     return ret;
   }




   public int getAllianceLeader(int allianceId) {
/*  67 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/*  68 */     if (alliance != null) {
/*  69 */       return alliance.getLeaderId();
     }
/*  71 */     return 0;
   }




   public void updateAllianceRanks(int allianceId, String[] ranks) {
/*  78 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/*  79 */     if (alliance != null) {
/*  80 */       alliance.setRank(ranks);
     }
   }




   public void updateAllianceNotice(int allianceId, String notice) {
/*  88 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/*  89 */     if (alliance != null) {
/*  90 */       alliance.setNotice(notice);
     }
   }




   public boolean canInvite(int allianceId) {
/*  98 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/*  99 */     return (alliance != null && alliance.getCapacity() > alliance.getNoGuilds());
   }




   public boolean changeAllianceLeader(int allianceId, int chrId) {
/* 106 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 107 */     return (alliance != null && alliance.setLeaderId(chrId));
   }




   public boolean changeAllianceLeader(int allianceId, int chrId, boolean sameGuild) {
/* 114 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 115 */     return (alliance != null && alliance.setLeaderId(chrId, sameGuild));
   }




   public boolean changeAllianceRank(int allianceId, int chrId, int change) {
/* 122 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 123 */     return (alliance != null && alliance.changeAllianceRank(chrId, change));
   }




   public boolean changeAllianceCapacity(int allianceId) {
/* 130 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 131 */     return (alliance != null && alliance.setCapacity());
   }




   public boolean disbandAlliance(int allianceId) {
/* 138 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 139 */     return (alliance != null && alliance.disband());
   }




   public boolean addGuildToAlliance(int allianceId, int guildId) {
/* 146 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 147 */     return (alliance != null && alliance.addGuild(guildId));
   }




   public boolean removeGuildFromAlliance(int allianceId, int guildId, boolean expelled) {
/* 154 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 155 */     return (alliance != null && alliance.removeGuild(guildId, expelled));
   }




   public void sendGuild(int allianceId) {
/* 162 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 163 */     if (alliance != null) {
/* 164 */       sendGuild(GuildPacket.getAllianceUpdate(alliance), -1, allianceId);
/* 165 */       sendGuild(GuildPacket.getGuildAlliance(alliance), -1, allianceId);
     }
   }

   public void sendGuild(byte[] packet, int exceptionId, int allianceId) {
/* 170 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 171 */     if (alliance != null) {
/* 172 */       for (int i = 0; i < alliance.getNoGuilds(); i++) {
/* 173 */         int guildId = alliance.getGuildId(i);
/* 174 */         if (guildId > 0 && guildId != exceptionId) {
/* 175 */           WorldGuildService.getInstance().guildPacket(guildId, packet);
         }
       }
     }
   }




   public boolean createAlliance(String allianceName, int chrId, int chrId2, int guildId, int guildId2) {
/* 185 */     int allianceId = MapleGuildAlliance.createToDb(chrId, allianceName, guildId, guildId2);
/* 186 */     if (allianceId <= 0) {
/* 187 */       return false;
     }
/* 189 */     MapleGuild leaderGuild = WorldGuildService.getInstance().getGuild(guildId);
/* 190 */     MapleGuild memberGuild = WorldGuildService.getInstance().getGuild(guildId2);
/* 191 */     leaderGuild.setAllianceId(allianceId);
/* 192 */     memberGuild.setAllianceId(allianceId);
/* 193 */     leaderGuild.changeARank(true);
/* 194 */     memberGuild.changeARank(false);

/* 196 */     MapleGuildAlliance alliance = getAlliance(allianceId);

/* 198 */     sendGuild(GuildPacket.createGuildAlliance(alliance), -1, allianceId);
/* 199 */     sendGuild(GuildPacket.getAllianceInfo(alliance), -1, allianceId);
/* 200 */     sendGuild(GuildPacket.getGuildAlliance(alliance), -1, allianceId);
/* 201 */     sendGuild(GuildPacket.changeAlliance(alliance, true), -1, allianceId);
/* 202 */     return true;
   }




   public void allianceChat(int guildId, String name, int chrId, String msg) {
/* 209 */     MapleGuild chrGuild = WorldGuildService.getInstance().getGuild(guildId);
/* 210 */     if (chrGuild != null) {
/* 211 */       MapleGuildAlliance alliance = getAlliance(chrGuild.getAllianceId());
/* 212 */       if (alliance != null) {
/* 213 */         for (int i = 0; i < alliance.getNoGuilds(); i++) {
/* 214 */           MapleGuild guild = WorldGuildService.getInstance().getGuild(alliance.getGuildId(i));
/* 215 */           if (guild != null) {
/* 216 */             guild.allianceChat(name, chrId, msg);
           }
         }
       }
     }
   }

   public void setNewAlliance(int guildId, int allianceId) {
/* 224 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 225 */     MapleGuild guild = WorldGuildService.getInstance().getGuild(guildId);
/* 226 */     if (alliance != null && guild != null) {
/* 227 */       for (int i = 0; i < alliance.getNoGuilds(); i++) {
/* 228 */         if (guildId == alliance.getGuildId(i)) {
/* 229 */           guild.setAllianceId(allianceId);
/* 230 */           guild.broadcast(GuildPacket.getAllianceInfo(alliance));
/* 231 */           guild.broadcast(GuildPacket.getGuildAlliance(alliance));
/* 232 */           guild.broadcast(GuildPacket.changeAlliance(alliance, true));
/* 233 */           guild.changeARank();
/* 234 */           guild.writeToDB(false);
         } else {
/* 236 */           MapleGuild guild_ = WorldGuildService.getInstance().getGuild(alliance.getGuildId(i));
/* 237 */           if (guild_ != null) {
/* 238 */             guild_.broadcast(GuildPacket.addGuildToAlliance(alliance, guild));
/* 239 */             guild_.broadcast(GuildPacket.changeGuildInAlliance(alliance, guild, true));
           }
         }
       }
     }
   }

   public void setOldAlliance(int guildId, boolean expelled, int allianceId) {
/* 247 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 248 */     MapleGuild guild_ = WorldGuildService.getInstance().getGuild(guildId);
/* 249 */     if (alliance != null) {
/* 250 */       for (int i = 0; i < alliance.getNoGuilds(); i++) {
/* 251 */         MapleGuild guild = WorldGuildService.getInstance().getGuild(alliance.getGuildId(i));
/* 252 */         if (guild == null) {
/* 253 */           if (guildId != alliance.getGuildId(i)) {
/* 254 */             alliance.removeGuild(guildId, false, true);

           }
         }
/* 258 */         else if (guild_ == null || guildId == alliance.getGuildId(i)) {
/* 259 */           guild.changeARank(5);
/* 260 */           guild.setAllianceId(0);
/* 261 */           guild.broadcast(GuildPacket.disbandAlliance(allianceId));
/* 262 */         } else if (guild_ != null) {
/* 263 */           guild.broadcast(MaplePacketCreator.serverNotice(5, "[" + guild_.getName() + "] 家族退出家族联盟."));
/* 264 */           guild.broadcast(GuildPacket.changeGuildInAlliance(alliance, guild_, false));
/* 265 */           guild.broadcast(GuildPacket.removeGuildFromAlliance(alliance, guild_, expelled));
         }
       }
     }
/* 269 */     if (guildId == -1) {
/* 270 */       this.lock.writeLock().lock();
       try {
/* 272 */         this.allianceList.remove(Integer.valueOf(allianceId));
       } finally {
/* 274 */         this.lock.writeLock().unlock();
       }
     }
   }

   public List<byte[]> getAllianceInfo(int allianceId, boolean start) {
/* 280 */     List<byte[]> ret = (List)new ArrayList<>();
/* 281 */     MapleGuildAlliance alliance = getAlliance(allianceId);
/* 282 */     if (alliance != null) {
/* 283 */       if (start) {
/* 284 */         ret.add(GuildPacket.getAllianceInfo(alliance));
/* 285 */         ret.add(GuildPacket.getGuildAlliance(alliance));
       }
/* 287 */       ret.add(GuildPacket.getAllianceUpdate(alliance));
     }
/* 289 */     return ret;
   }

   public void save() {
/* 293 */     System.out.println("正在保存家族联盟数据...");
/* 294 */     this.lock.writeLock().lock();
     try {
/* 296 */       for (MapleGuildAlliance alliance : this.allianceList.values()) {
/* 297 */         alliance.saveToDb();
       }
     } finally {
/* 300 */       this.lock.writeLock().unlock();
     }
   }

   private static class SingletonHolder
   {
/* 306 */     protected static final WorldAllianceService instance = new WorldAllianceService();
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\WorldAllianceService.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */